package com.sunronsoft.mind.map;

import com.jfinal.kit.Kv;
import com.jfinal.kit.LogKit;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author Sunron
 **/
public class DataKit {
    
    private DataKit() {
    } 
    
    public static void processDirectory(String path) {
        try {
            File file = new File(path);
            if (!file.exists() || !file.isDirectory()) {
                file.mkdirs();
            }
        } catch (Exception e) {
            LogKit.error(e.getMessage(), e);
        }
    }
    
    public static String readJsonFile(String fileName) {
        String jsonStr = null;
        BufferedReader reader = null;
        try {
            File jsonFile = new File(fileName);
            if (jsonFile.exists() && jsonFile.isFile()) {
                InputStreamReader streamReader = new InputStreamReader(new FileInputStream(jsonFile), StandardCharsets.UTF_8);
                reader = new BufferedReader(streamReader);
                
                StringBuilder sb = new StringBuilder();
                String line = null;                
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                jsonStr = sb.toString();
                LogKit.info("读取数据成功：" + fileName);
            }
        } catch (Exception e) {
            LogKit.error(e.getMessage(), e);
        } finally {
            try {
                if (reader != null) reader.close();
            } catch (Exception e) {
                LogKit.error(e.getMessage(), e);
            }
        }
        
        return jsonStr;
    }
    
    public static boolean writeJsonFile(String fileName, String data) {
        BufferedWriter writer = null;
        File jsonFile = new File(fileName);
        if (!jsonFile.exists()) {
            try {
                jsonFile.createNewFile();
            } catch (Exception e) {
                LogKit.error(e.getMessage(), e);
                return false;
            }
        }
        
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(jsonFile), StandardCharsets.UTF_8));
            writer.write(data);
            writer.flush();
            
            return true;
        } catch (Exception e) {
            LogKit.error(e.getMessage(), e);
            return false;
        } finally {
            try {
                if (writer != null) writer.close();
            } catch (Exception e) {
                LogKit.error(e.getMessage(), e);
            }
        }
    }
    
    public static Kv toFail(String msg) {
        return toFail(-1, msg);
    }
    
    public static Kv toFail(int code, String msg) {
        return Kv.by("code", code).set("msg", msg);
    }
    
    public static Kv toSuccess(Object data) {
        return Kv.by("code", 0).set("msg", "处理成功").set("data", data);
    }

    public static Kv toSuccess(int code, String msg) {
        return Kv.by("code", code).set("msg", msg);
    }
}
